package com.ruoyi.web.controller.system;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.excel.EasyExcel;
import com.ruoyi.system.domain.PatientCaseInfo;
import com.ruoyi.system.excel.ExcelUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Diagnose;
import com.ruoyi.system.service.IDiagnoseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 诊断Controller
 * 
 * @author lwb
 * @date 2024-08-21
 */
@RestController
@RequestMapping("/system/diagnose")
public class DiagnoseController extends BaseController
{
    @Autowired
    private IDiagnoseService diagnoseService;

    /**
     * 查询诊断列表
     */
    //@PreAuthorize("@ss.hasPermi('system:diagnose:list')")
    @GetMapping("/list")
    public TableDataInfo list(Diagnose diagnose)
    {
        startPage();
        List<Diagnose> list = diagnoseService.selectDiagnoseList(diagnose);
        return getDataTable(list);
    }

    /**
     * 导出诊断列表
     */
    @PreAuthorize("@ss.hasPermi('system:diagnose:export')")
    @Log(title = "诊断", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Diagnose diagnose)
    {
        List<Diagnose> list = diagnoseService.selectDiagnoseList(diagnose);
        ExcelUtil<Diagnose> util = new ExcelUtil<Diagnose>(Diagnose.class);
        util.exportExcel(response, list, "病案信息诊断管理");
    }

    /**
     * 获取诊断详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:diagnose:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(diagnoseService.selectDiagnoseById(id));
    }

    /**
     * 新增诊断
     */
    @PreAuthorize("@ss.hasPermi('system:diagnose:add')")
    @Log(title = "诊断", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Diagnose diagnose)
    {
        return toAjax(diagnoseService.insertDiagnose(diagnose));
    }

    /**
     * 修改诊断
     */
    @PreAuthorize("@ss.hasPermi('system:diagnose:edit')")
    @Log(title = "诊断", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Diagnose diagnose)
    {
        return toAjax(diagnoseService.updateDiagnose(diagnose));
    }

    /**
     * 删除诊断
     */
    @PreAuthorize("@ss.hasPermi('system:diagnose:remove')")
    @Log(title = "诊断", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(diagnoseService.deleteDiagnoseByIds(ids));
    }

    @PostMapping("/importTemplate")
    @PreAuthorize("@ss.hasPermi('system:diagnose:importTemplate')")
    public void importTemplate(HttpServletResponse response) throws IOException {
        //模版路径
        InputStream resourceAsStream = ExcelUtils.class.getClassLoader().getResourceAsStream("excel-template/diagnose.xlsx");

        // 设置 header 和 contentType。写在最后的原因是，避免报错时，响应 contentType 已经被修改了
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("病案信息诊断管理导入模版.xlsx", "UTF-8"));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");

        EasyExcel.write(response.getOutputStream()).withTemplate(resourceAsStream).sheet().doFill(CollUtil.newHashMap());
    }

    @Log(title = "诊断导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:diagnose:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<Diagnose> excelUtil = new ExcelUtil<Diagnose>(Diagnose.class);
        List<Diagnose> diagnoseList = excelUtil.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = diagnoseService.importCase(diagnoseList, updateSupport, operName);
        return success(message);
    }

}
